package com.sangmo.fts.common.authority.sql;

import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class WhereModifier {
    private String sql = "";
    private Map<String, Object> variables = new HashMap<>();

    public WhereModifier() {

    }

    public WhereModifier(String sql, Map<String, Object> variables) {
        this.sql = sql;
        this.variables = variables;
    }

    public String getSql() {
        if (StringUtils.isNotEmpty(sql)) {
            return "(" + sql + ")";
        }

        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }

    public Map<String, Object> getVariables() {
        return variables;
    }

    public void setVariables(Map<String, Object> variables) {
        this.variables = variables;
    }

    public static String concat(String where1, String where2, boolean isOr) {
        if (StringUtils.isEmpty(where1)) {
            return where2;
        }
        if (StringUtils.isEmpty(where2)) {
            return where1;
        }
        return String.format("%s %s %s", where1, isOr? "OR" : "AND", where2);
    }
}
